/* tcp.c - TCP/IP fucntion create the connection to the FTP server. */

#include "tcp.h"

/* Connect to TCP/IP socket */
FILE * tcp_connect(char *server, char *port, char *mode) {
	struct addrinfo ai, *srv = NULL, *p = NULL;
	struct timeval timeout;

	/* Set time-out*/
	timeout.tv_sec = 1;
	timeout.tv_usec = 0; /* 800000 micro-second (0.8 secound) */

	memset(&ai, 0, sizeof(struct addrinfo));
	if (( getaddrinfo(server, port, &ai, &srv)) != 0 ) {
		print(PERROR, TRUE,"Cannot resolve %s - (%s)", server,gai_strerror(1));
	}

	ai.ai_family = AF_UNSPEC;
	ai.ai_socktype = SOCK_STREAM;
	ai.ai_protocol = IPPROTO_TCP;

	p = srv;
	if (( getnameinfo((struct sockaddr *)p->ai_addr,
					p->ai_addrlen, 
					ip, sizeof(ip),
					NULL, (socklen_t) 0U, 
					NI_NUMERICHOST)) != 0)
		print(PERROR, TRUE,"Cannot resolve %s- (%s).", server,gai_strerror(1));

	if (( fd = socket(p->ai_family, p->ai_socktype, 0)) < 0 )
		print(PERROR, TRUE,"Failed to create socket.");

	if ( connect(fd, p->ai_addr, p->ai_addrlen) < 0 )
		print(PERROR, TRUE,"Failed to connect.");
	freeaddrinfo(srv);


	if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
				sizeof(timeout)) < 0)
		print(PERROR, TRUE,"Socket timeout.");
	if (setsockopt (fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
				sizeof(timeout)) < 0)
		print(PERROR, TRUE,"Socket timeout.");

	if (strcmp(mode, "wb") == 0)
	{
		int opt_len = sizeof(int);
		int send_size = FTP_SOCKET_SEND_BUFF_SIZE / 2;
		setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const char *)&send_size, opt_len);
		getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *)&send_size, (socklen_t *)&opt_len);
		printf("ftp sock send buf size = %u\n", send_size);
	}
	
	return (fdopen(fd, mode));
}

